package com.pactera.mavenweb.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pactera.mavenweb.dao.BookDao;
import com.pactera.mavenweb.dto.Page;
import com.pactera.mavenweb.entity.Book;
import com.pactera.mavenweb.service.BookService;

@Service("bookService")
public class BookServiceImpl implements BookService {
	
	/**
	 * 注入dao
	 */
	@Resource(name = "bookDao")
	private BookDao bookDao;
		
//	@Override
	public int saveBooks(List<Book> books) {
		try {
			for (Book Book : books) {
				bookDao.saveBook(Book);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return 0;
	}
	
//	@Override
	public int getAmountForAllBook() {
		return bookDao.queryAmountForAll();
	}
	
//	@Override
	public Book getBook(int id) {
		return bookDao.queryById(id);
	}
	
//	@Override
	public Book getBookByName(String bookname) {
		return bookDao.queryByName(bookname);
		
	}
	
//	@Override
	public List<Book> getAllBook() {
		return bookDao.queryAll();
	}
	
//	@Override
	public List<Book> getAllBookByPageOrder(Page page) {
		return bookDao.queryAllByPageOrder(page);
	}
	
////	@Override
	public void addBook(Book book) {
		bookDao.saveBook(book);
	}
	
	
////	@Override
	public boolean delBook(int id) {
		
		bookDao.delete(id);
		return true;
	}
	
////	@Override
	public void updateBook(Book book) {
		Book currentBook = bookDao.queryById(book.getId());
		currentBook.setName(book.getName());
		currentBook.setBorrowed(book.isBorrowed());
		currentBook.setRemarks(book.getRemarks());
		bookDao.update(currentBook);
	}
}